package end.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import end.bean.LogInfo;
import end.ser.LogInfoSER;
import end.util.export.excel.DownloadUtil;
import end.util.export.excel.ExcelDto;
import end.util.export.excel.ExcelUtil;

/**
 * 
 * ManagerForController.java 2016-6-24
 * 
 * Copyright zhaocj Inc. All rights reserved. Love Me Like Love Justin Bieber
 */
@Controller
@RequestMapping("/logInfo")
public class LogInfoForController {
	private final static Logger logger = LogManager
			.getLogger(LogInfoForController.class);
	@Autowired
	public LogInfoSER<LogInfo> logInfoSER;

	public LogInfoSER<LogInfo> getLogInfoSER() {
		return logInfoSER;
	}

	public void setLogInfoSER(LogInfoSER<LogInfo> logInfoSER) {
		this.logInfoSER = logInfoSER;
	}

	@RequestMapping("/queryLogInfosByPage")
	public @ResponseBody
	Map<String, Object> queryLogInfosByPage(int page, int rows, String sort,
			String order, LogInfo logInfo) throws Exception {
		logInfo.getPageUtil().setPaging(true);
		logInfo.getPageUtil().setOrderByCondition(sort);
		logInfo.getPageUtil().setQueryCondition(order);
		logInfo.getPageUtil().setPageOffset((page - 1) * rows);
		logInfo.getPageUtil().setPageSize(rows);
		List<LogInfo> beans = logInfoSER.selectByBean(logInfo);
		Map<String, Object> rets = new HashMap<String, Object>();
		rets.put("total", logInfoSER.selectByBeanCount(logInfo));
		rets.put("rows", beans);
		return rets;
	}

	@RequestMapping("/exportExcel")
	public void exportSysLogExcel(HttpServletResponse response,
			HttpServletRequest request, LogInfo logInfo) throws Exception {
		List<LogInfo> exportSysLogExcel = logInfoSER.exportSysLogExcel(logInfo);
		List<Object> obs = new ArrayList<Object>();
		for (LogInfo logInfoEntry : exportSysLogExcel) {
			obs.add(logInfoEntry);
		}
		if (exportSysLogExcel.size() != 0) {
			List<ExcelDto> excelDtos = ExcelUtil.objects2ExcelDtos(obs);
			String fileLocation = ExcelUtil.xlsDto2Excel(excelDtos);
			DownloadUtil.exportToBrowser(request, response, fileLocation,
					"日志.xls");
		}
	}
}
